<template>
  <div class="orders-page">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" clearable>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-table :data="orderList" border stripe>
        <el-table-column type="index"></el-table-column>
        <el-table-column label="订单标号" prop="order_number"></el-table-column>
        <el-table-column
          label="订单价格"
          prop="order_price"
          width="80px"
        ></el-table-column>
        <el-table-column label="是否付款" prop="pay_status" width="80px">
          <template #default="{ row }">
            <el-tag type="success" v-if="row.pay_status === '1'">已付款</el-tag>
            <el-tag type="danger" v-else>未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="是否发货"
          prop="is_send"
          width="80px"
        ></el-table-column>
        <el-table-column label="下单时间" prop="create_time" width="200px">
          <template #default="{ row }">
            {{ row.create_time | relativeTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template #default="{ row }">
            <div class="actions">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="small"
                @click="showEditAddress(row.order_id)"
              ></el-button>
              <el-button
                type="success"
                icon="el-icon-location"
                size="small"
                @click="showProgress"
              ></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="queryObj.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryObj.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @current-change="changePage"
        @size-change="handleSizeChange"
      >
      </el-pagination>
    </el-card>

    <el-dialog title="修改订单数据" :visible.sync="showEditVisible" width="50%">
      <el-form
        :model="orderForm"
        :rules="orderRules"
        ref="orderForm"
        label-width="100px"
      >
        <el-form-item label="订单价格" prop="price">
          <el-input v-model="orderForm.order_price" type="number"></el-input>
        </el-form-item>
        <el-form-item label="是否付款">
          <el-radio v-model="orderForm.pay_status" label="1">已付款</el-radio>
          <el-radio v-model="orderForm.pay_status" label="0">未付款</el-radio>
        </el-form-item>
        <el-form-item label="是否发货">
          <el-radio v-model="orderForm.is_send" label="1">是</el-radio>
          <el-radio v-model="orderForm.is_send" label="0">否</el-radio>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showEditVisible = false">取 消</el-button>
        <el-button type="primary" @click="editOrder">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog title="物流进度" :visible.sync="showProgressVisible" width="50%">
      <el-timeline :reverse="false">
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.ftime"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
      <!--      <span slot="footer" class="dialog-footer">-->
      <!--        <el-button @click="showProgressVisible = false">取 消</el-button>-->
      <!--        <el-button type="primary" @click="editOrder">确 定</el-button>-->
      <!--      </span>-->
    </el-dialog>
  </div>
</template>

<script>
import {
  editOrderInfoApi,
  getOrderInfoApi,
  getOrdersListApi,
  showProgressApi,
} from "@/api/orders";
export default {
  name: "orderList",
  data() {
    return {
      orderList: [],
      total: 0,
      showEditVisible: false,
      showProgressVisible: false,
      orderId: "",
      progressInfo: [],
      orderForm: {
        order_price: 0,
        pay_status: "1",
        is_send: "0",
      },
      orderRules: {
        order_price: [
          {
            required: true,
            message: "请填写价格",
            trigger: "blur",
          },
        ],
      },
      queryObj: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
    };
  },
  created() {
    this.getOrdersListApi();
  },
  methods: {
    async getOrdersListApi() {
      const res = await getOrdersListApi(this.queryObj);
      // console.log(res);
      if (res.meta.status === 200) {
        this.orderList = res.data.goods;
        this.total = res.data.total;
      } else {
        this.$message.error("获取订单数据失败");
      }
    },
    handleSizeChange(newSize) {
      this.queryObj.pagesize = newSize;
      this.getOrdersListApi();
    },
    changePage(newPage) {
      this.queryObj.pagenum = newPage;
      this.getOrdersListApi();
    },
    async showEditAddress(id) {
      this.showEditVisible = true;
      this.orderId = id;
      const res = await getOrderInfoApi(id);
      // console.log(res);
      if (res.meta.status === 200) {
        this.orderForm.order_price = res.data.order_price;
        this.orderForm.is_send = res.data.is_send === "是" ? "1" : "0";
        this.orderForm.pay_status = res.data.pay_status;
      } else {
        this.$message.error("获取数据失败");
      }
    },
    async editOrder() {
      await this.$refs.orderForm.validate();
      try {
        const res = await editOrderInfoApi(this.orderId, this.orderForm);
        if (res.meta.status === 201) {
          this.$message.success("更新成功");
          this.getOrdersListApi();
          this.showEditVisible = false;
        } else {
          this.$message.error(res.meta.msg);
        }
      } catch (err) {
        this.$message.error("系统繁忙");
      }
    },
    async showProgress() {
      this.showProgressVisible = true;
      const res = await showProgressApi();
      console.log(res);
      this.progressInfo = res.data;
    },
  },
};
</script>

<style lang="scss" scoped>
.el-breadcrumb {
  margin-bottom: 15px;
}
.el-input {
  margin-bottom: 15px;
}
</style>
